package com.anxuan.agent.converter;

import com.anxuan.agent.domain.ChatMessage;
import org.springframework.ai.chat.messages.*;

import java.util.List;
import java.util.Map;

/**
 * @author gaoxiaoqi
 * @Date 2025/5/28 14:25
 */
public class MessageConverter {

    public static ChatMessage toChatMessage(Message message, String conversationId){
        return ChatMessage.builder()
                .conversationId(conversationId)
                .messageType(message.getMessageType())
                .content(message.getText())
                .metadata(message.getMetadata())
                .build();
    }

    public static Message toMessage(ChatMessage chatMessage){

        MessageType messageType = chatMessage.getMessageType();
        String text = chatMessage.getContent();
        Map<String, Object> metadata = chatMessage.getMetadata();

        return switch (messageType){
            case MessageType.USER -> new UserMessage(text);
            case MessageType.ASSISTANT -> new AssistantMessage(text, metadata);
            case MessageType.SYSTEM -> new SystemMessage(text);
            case MessageType.TOOL -> new ToolResponseMessage(List.of(), metadata);
        };
    }
}
